<?php
if ($ticket_parent!=1) {
	header('Location: class_manager.php');
	exit;
}

if ($_POST["register_class_form_submit"] && is_logged_in()){
	$character_name=mysql_real_escape_string(trim($_POST["register_class_form_char_name"]), $class_manager_db);
	if(strlen($character_name) <= 0){
		e("You must enter a character name in order to register.");
	}
	
	$class_pkey=mysql_real_escape_string(trim($_POST["register_class_form_class"]), $class_manager_db);
	if(strlen($class_pkey) <= 0){
		e("Critical Error: Invalid class pkey when regitering.");
	}
	$result = mysql_query("SELECT module, capacity, UNIX_TIMESTAMP(reg_start) AS reg_start FROM classes WHERE pkey='$class_pkey'", $class_manager_db) or die(mysql_error($class_manager_db));
	if(mysql_num_rows($result) <= 0){
		e("Critical Error: Class pkey not found in database.");
	}
	else{
		$line=mysql_fetch_array($result,MYSQL_ASSOC);
		$module_pkey = $line["module"];
		$class_capacity = $line["capacity"];
	}
	
	if($line["reg_start"] != NULL && $line["reg_start"] > time()){
		e("Registration has not yet opened for this class");
	}
	
	$participant_status = false;
	$participant_alumnus = 0;
	if(isset($_POST["register_class_form_alumnus"]) && $_POST["register_class_form_alumnus"] == "1" && isset($module_pkey)){
		$result = mysql_query("SELECT classes.module FROM completed LEFT JOIN classes ON completed.class=classes.pkey WHERE completed.name='$character_name'", $class_manager_db) or die(mysql_error($class_manager_db));
		while($line = mysql_fetch_array($result,MYSQL_ASSOC)){
			if($line["module"] == $module_pkey){
				$participant_status = "registered";
				$participant_alumnus = 1;
				break;
			}
		}
		
		if($participant_alumnus == 0){
			e("We have no record as you being an alumnus of this class, please check the spelling or register as a student.");
		}
	}
	else if(isset($module_pkey)){ //check capacity and prereqs
		//checking prereqs
		$prerequisites = array();
		$result = mysql_query("SELECT prerequisite FROM prerequisites WHERE module='$module_pkey'", $class_manager_db) or die(mysql_error($class_manager_db));
		while($line = mysql_fetch_array($result,MYSQL_ASSOC)){
			$prerequisites[((string)$line["prerequisite"])] = 0;
		}
		$result = mysql_query("SELECT classes.module FROM completed LEFT JOIN classes ON completed.class=classes.pkey WHERE completed.name='$character_name'", $class_manager_db) or die(mysql_error($class_manager_db));
		while($line = mysql_fetch_array($result,MYSQL_ASSOC)){
			if(array_key_exists(((string)$line["module"]), $prerequisites)){
				$prerequisites[((string)$line["module"])] = 1;
			}
		}
		foreach($prerequisites as $met){
			if($met==0){
				e("All prerequisites have not yet been met, you must complete these before registering for this class.");
				break;
			}
		}
		
		if(empty($errors)){
			$result = mysql_query("SELECT pkey FROM participants WHERE class='$class_pkey' AND (status='registered' OR status='wait_list')", $class_manager_db) or die(mysql_error($class_manager_db));
			$num_registered = mysql_num_rows($result);
			if($class_capacity == NULL || $num_registered<$class_capacity){
				$participant_status="registered";
			}
			else{
				$participant_status="wait_list";
			}
			
			do{
				$salt = rand();
				$payment_code = hash("sha512",$salt.$_SESSION["user"]["member_id"].stripslashes($character_name).$class_pkey);
				$result = mysql_query("SELECT pkey FROM participants WHERE payment_code='$payment_code'", $class_manager_db) or die(mysql_error($class_manager_db));
			}while(mysql_num_rows($result)>0);
		}
	}
	else{
		e("Critical Error: unhandled case!");
	}
	
	//check for duplicates
	$result = mysql_query("SELECT pkey FROM participants WHERE character_name='$character_name' AND class='$class_pkey'", $class_manager_db) or die(mysql_error($class_manager_db));
	if(mysql_num_rows($result)>0){
		e("This character is already registered for this class.");
	}
	
	if(empty($errors)){
		mysql_query("LOCK TABLE participants WRITE", $class_manager_db) or die(mysql_error($class_manager_db));
		
		mysql_query("INSERT INTO participants (forum_member_id,character_name,class,status,alumnus) VALUES ('".$_SESSION["user"]["member_id"]."', '$character_name', '$class_pkey', '$participant_status', '$participant_alumnus')", $class_manager_db) or die(mysql_error($class_manager_db));
		$participant_pkey = mysql_insert_id($class_manager_db);
		if(isset($salt) && isset($payment_code)){
			mysql_query("UPDATE participants SET salt='$salt', payment_code='$payment_code' WHERE pkey='$participant_pkey'", $class_manager_db) or die(mysql_error($class_manager_db));
		}
		
		mysql_query("UNLOCK TABLES", $class_manager_db) or die(mysql_error($class_manager_db));
		
		s("Registration Successful.'");
	}
}

?>